package com.fingard.dsp.bank.directbank.boc01;

import com.fingard.dsp.bank.directbank.bankInterface.IPushInfoToBank;
import com.fingard.dsp.bank.reqrespobj.pushinfotobank.ReqPushTransInfo;
import com.fingard.dsp.bank.reqrespobj.pushinfotobank.RetAtsTransResult;
import com.fingard.dsp.bank.reqrespobj.pushinfotobank.detail.RetAtsTransResultDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOC01PushInfoToBank extends BOC01Base implements IPushInfoToBank {
    @Override
    public RetAtsTransResult pushTransInfoToBank(ReqPushTransInfo p_transInfo) throws Exception {
        RetAtsTransResult retAts = new RetAtsTransResult(p_transInfo.reqHeader);
        RetAtsTransResultDetail tmpRetDetail = new RetAtsTransResultDetail();
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0475";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0475-rq");
            while (p_transInfo.readRecord()) {
                XElement tmpXElement = new XElement("b2e0475-rq", "");
                tmpXElement.addChild(new XElement("firmno", "02"));//厂商编号
                tmpXElement.addChild(new XElement("cifno", bankFront.org));//客户号
                tmpXElement.addChild(new XElement("insid", p_transInfo.singleDetail.reqSeqID));//业务编号
                tmpXElement.addChild(new XElement("transdate", p_transInfo.singleDetail.tranDate.replace("-", "")));//交易日期
                tmpXElement.addChild(new XElement("transtime", p_transInfo.singleDetail.tranTime));//交易时间
                tmpXElement.addChild(new XElement("bankcode", "003"));//中行账号银行号

                String actacn = "";//中行账号
                String actname = "";//中行账号名称
                String bocorgnm = "";//中行账号开户机构名称
                String otheractacn = "";//他行账号
                String otheractname = "";//他行账号名称
                String cnaps = "";//他行CNAPS号
                String swift = "";//他行SWIFT号
                String otherbkname = "";//他行账号开户机构名称
                String otherbktyp = "";//他行类型
                String transtyp = "";//交易类型
                String cdflag = "";//借贷标识 D-借 C-贷
                if ("中国银行".equals(p_transInfo.singleDetail.payBankName)) {
                    actacn = p_transInfo.singleDetail.payAct;
                    actname = p_transInfo.singleDetail.payName;
                    bocorgnm = p_transInfo.singleDetail.payBnk;
                    otheractacn = p_transInfo.singleDetail.recAct;
                    otheractname = p_transInfo.singleDetail.recName;
                    otherbkname = p_transInfo.singleDetail.recBnk;
                    otherbktyp = getBankConfig().getMapValueByKey("pushInfoBankMapping", p_transInfo.singleDetail.recBankName);
                    cdflag = "C";
                } else {
                    actacn = p_transInfo.singleDetail.recAct;
                    actname = p_transInfo.singleDetail.recName;
                    bocorgnm = p_transInfo.singleDetail.recBnk;
                    otheractacn = p_transInfo.singleDetail.payAct;
                    otheractname = p_transInfo.singleDetail.payName;
                    otherbkname = p_transInfo.singleDetail.payBnk;
                    otherbktyp = getBankConfig().getMapValueByKey("pushInfoBankMapping", p_transInfo.singleDetail.payBankName);
                    cdflag = "D";
                }

                cnaps = p_transInfo.singleDetail.cNAPSCode;
                transtyp = getBankConfig().getMapValueByKey("pushInfoPayTypeMapping", p_transInfo.singleDetail.payType);
                tmpXElement.addChild(new XElement("actacn", actacn));//中行账号
                tmpXElement.addChild(new XElement("actname", actname));//中行账号名称
                tmpXElement.addChild(new XElement("bocorgnm", bocorgnm));//中行账号开户机构名称
                tmpXElement.addChild(new XElement("otheractacn", otheractacn));//他行账号
                tmpXElement.addChild(new XElement("otheractname", otheractname));//他行账号名称
                tmpXElement.addChild(new XElement("cnaps", cnaps));//他行CNAPS号
                tmpXElement.addChild(new XElement("swift", swift));//他行SWIFT号
                tmpXElement.addChild(new XElement("otherbkname", otherbkname));//他行账号开户机构名称
                tmpXElement.addChild(new XElement("otherbktyp", otherbktyp));//他行类型
                tmpXElement.addChild(new XElement("transtyp", transtyp));//交易类型
                tmpXElement.addChild(new XElement("cdflag", cdflag));//借贷标识 D-借 C-贷
                tmpXElement.addChild(new XElement("amt", p_transInfo.singleDetail.payAmount));//金额
                tmpXElement.addChild(new XElement("curcode", "CNY"));//币种
                tmpXElement.addChild(new XElement("furinfo", p_transInfo.singleDetail.usage));//用途
                tmpXElement.addChild(new XElement("transstatus", "01"));//交易状态
                xmlWriter.writeXElement(tmpXElement);
            }
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0475-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0475-rs/status/rspmsg");
                    if (isActiveRetCode(tmprspcod)) {
                        if (tmpBkReader.read("/bocb2e/trans/trn-b2e0475-rs/status")) {
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = tmpBkReader.readedXElement.getSingleTextNotNull("rspmsg");
                            tmpRetDetail.transState = tmpBkReader.readedXElement.getSingleTextNotNull("rspcod");
                            tmpRetDetail.payInfoCode = tmpBkReader.readedXElement.getSingleTextNotNull("rspcod");
                        }
                    } else {
                        if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                            tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                        }
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmprspmsg;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
}
